﻿using System;
using System.Collections.Generic;

namespace Rabbit.Logic
{
    public class NoDb
    {
        public static CartTotal ComputeCartTotal(List<ShopCartItemFull> list)
        {
            CartTotal result = new CartTotal();
            foreach (ShopCartItemFull shopCartItem in list)
            {
                result.TotalNumber += shopCartItem.Quantity;
                Product product = shopCartItem.ProductInfo;
                result.TotalWeight += shopCartItem.Quantity * product.GrossWeight;
                result.TotalPrice += shopCartItem.Quantity * product.PricePerUnit;
            }
            return result;
        }

        public static CartTotal ComputeCartTotal(List<ProductSnapWithQuantity> list)
        {
            CartTotal result = new CartTotal();
            foreach (ProductSnapWithQuantity item in list)
            {
                result.TotalNumber += item.Quantity;
                ProductSnap product = item.Product;
                result.TotalWeight += item.Quantity * product.GrossWeight;
                result.TotalPrice += item.Quantity * product.PricePerUnit;
            }
            return result;
        }

        public static DiliveryPriceResult ComputeDiliveryCost(decimal freeShipAt, CartTotal total, ShipRule rule, Address address, Address shopAddress, bool isNear)
        {
            DiliveryPriceResult result = new DiliveryPriceResult() { Price = 0m, Reason = "" };

            if (isNear && address.Region == shopAddress.Region && address.City == shopAddress.City)
            {
                result.Reason = "店铺附近免费配送";
                return result;
            }

            if (total.TotalPrice > freeShipAt)
            {
                result.Reason = string.Format("满{0}包邮", freeShipAt);
                return result;
            }

            if (rule.CityRules.ContainsKey(address.City))
            {
                result.Price = rule.CityRules[address.City];
                result.Reason = string.Format("{0}运费{1}", address.City, result.Price);
                return result;
            }

            result.Price = rule.Rules[address.Province];
            result.Reason = string.Format("{0}运费{1}", address.Province, result.Price);

            return result;
        }

        public static string GetSnapIndex(Product product)
        {
            string result = string.Format("{0}__{1}", product.Id, product.LastModifiedDate.Value.Ticks);
            return result;
        }

        public static ProductSnap GetProductSnap(Product product)
        {
            ProductSnap result=new ProductSnap();
            result.Description = product.Description;
            result.GrossWeight = product.GrossWeight;
            result.LastModifiedDate = product.LastModifiedDate;
            result.MainImage = product.MainImage;
            result.Name = product.Name;
            result.NetWeight = product.NetWeight;
            result.PricePerUnit = product.PricePerUnit;
            result.ProductId = product.Id;
            result.ProductionDate = product.ProductionDate;
            result.ShopId = product.ShopId;
            result.SnapIndex = string.Format("{0}__{1}", product.Id, product.LastModifiedDate.Value.Ticks);
            result.SubTitle = product.SubTitle;
            result.Title = product.Title;

            return result;
        }
    }
}
